<?php

function meleeOutput($item_stat) {
 echo $item_stat["item_id"] . "\t";
 echo $item_stat["name"] . "\t";
 echo $item_stat["type"] . "\t";
 echo $item_stat["wear"] . "\t";
 echo $item_stat["str"] . "\t";
 echo $item_stat["ap"] . "\t";
 echo $item_stat["agi"] . "\t";
 echo $item_stat["hitr"] . "\t";
 echo $item_stat["critr"] . "\t";
 echo $item_stat["sta"] . "\t";
 echo $item_stat["res"] . "\t";
 echo $item_stat["def"] . "\t";

 //armor
 echo $item_stat["arm"] . "\t";

 //parry
 echo $item_stat["parry"] . "\t";
 //dodge
 echo $item_stat["dodge"] . "\t";
 //block rating
 echo $item_stat["blockr"] . "\t";
 //block value
 echo $item_stat["blockv"] . "\t";

 echo $item_stat["red"] . "\t";
 echo $item_stat["yel"] . "\t";
 echo $item_stat["blue"] . "\t";
 echo $item_stat["meta"] . "\t";
 echo "\n";
}

function getItemStat($itemtext, $verbose=0) {
  //ignore set bonuses
  $loc = strpos($itemtext, "pieces");
  if($loc) {
    $itemtext = substr($itemtext, 0, $loc);
  }
  if($verbose) {
    echo $itemtext . "\n";
  }
  
  preg_match_all("(Plate|Cloth|Leather|Mail)", $itemtext, $match);
  $item_stat["type"] = $match[0][0];
    
 preg_match_all("(Head|Neck|Shoulder|Chest|Back|Tabard|Shirt|Wrist|Hands|Waist|Legs|Feet|Finger|Two-Hand|Trinket|Off Hand|Held In Off-hand|Main Hand|One-Hand|Ranged|Bow|Gun|Crossbow|Shield|Thrown)", $itemtext, $match);
 $item_stat["wear"] = $match[0][0];

 preg_match_all("(\+([0-9]+) Agility)", $itemtext, $match);
 $item_stat["agi"] = $match[1][0];

 preg_match_all("(\+([0-9]+) Stamina)", $itemtext, $match);
 $item_stat["sta"] = $match[1][0];

 preg_match_all("(\+([0-9]+) Intellect)", $itemtext, $match);
 $item_stat["int"] = $match[1][0];

 preg_match_all("(\+([0-9]+) Strength)", $itemtext, $match);
 $item_stat["str"] = $match[1][0];

 preg_match_all("(\+([0-9]+) Spirit)", $itemtext, $match);
 $item_stat["spi"] = $match[1][0];

 preg_match_all("(Increases attack power by ([0-9]+).)", $itemtext, $match);
 $item_stat["ap"] = $match[1][0];

 preg_match_all("(Improves hit rating by ([0-9]+).)", $itemtext, $match);
 $item_stat["hitr"] = $match[1][0];

 preg_match_all("(Increases your hit rating by ([0-9]+).)", $itemtext, $match);
 $item_stat["hitr"] += $match[1][0];

 preg_match_all("(Improves critical strike rating by ([0-9]+).)", $itemtext, $match);
 $item_stat["critr"] = $match[1][0];

 preg_match_all("(Increases your critical strike rating by ([0-9]+).)", $itemtext, $match);
 $item_stat["critr"] += $match[1][0];

 preg_match_all("(Improves your resilience rating by ([0-9]+).)", $itemtext, $match);
 $item_stat["res"] += $match[1][0];

 preg_match_all("(Increases defense rating by ([0-9]+).)", $itemtext, $match);
 $item_stat["def"] += $match[1][0];

 preg_match_all("(([0-9]+) Armor)", $itemtext, $match);
 $item_stat["arm"] += $match[1][0];

 preg_match_all("(Increases your parry rating by ([0-9]+).)", $itemtext, $match);
 $item_stat["parry"] += $match[1][0];

 preg_match_all("(Increases your dodge rating by ([0-9]+).)", $itemtext, $match);
 $item_stat["dodge"] += $match[1][0];

 preg_match_all("(Increases your shield block rating by ([0-9]+).)", $itemtext, $match);
 $item_stat["blockr"] += $match[1][0];

 preg_match_all("(Increases the block value of your shield by ([0-9]+).)", $itemtext, $match);
 $item_stat["blockv"] += $match[1][0];

 //count the number of Red Socket, Yellow Socket, Blue Socket
 $item_stat["red"] = substr_count($itemtext, "Red Socket");
 $item_stat["yel"] = substr_count($itemtext, "Yellow Socket");
 $item_stat["blue"] = substr_count($itemtext, "Blue Socket");
 $item_stat["meta"] = substr_count($itemtext, "Meta Socket");
 
 //@todo: socket bonus!
 
 //@todo: plate, cloth?

 return $item_stat;
}
?>